<template>
  <div class="Manufacturer_detail  form_detail_page">
    <one-title :title="$route.query.id ? '外协厂商详情' : '外协厂商新增'" @refreshData="refresh"></one-title>
    <el-form ref="listForm" @submit.native.prevent :model="detailData" :rules="rules" v-if="!isEmpty&&!isLoading"
             v-loading="isLoading">
      <two-title title="基本信息"></two-title>
      <div class="from_panel">
        <div class="form_col">
          <el-form-item class="form_item" label="外协单位编号:" prop="number">
            <div class="input_wapper">
              <el-input
                size="small"
                placeholder="请输入外协单位编号"
                v-model.trim="detailData.number"
                clearable>
              </el-input>
            </div>
            <div class="clear_panel"></div>
          </el-form-item>
          <el-form-item class="form_item" label="外协单位名称:" prop="name">
            <div class="input_wapper">
              <el-input
                placeholder="外协单位名称"
                v-model.trim="detailData.name"
                clearable>
              </el-input>
            </div>
          </el-form-item>
        </div>
        <div class="form_col">
          <el-form-item class="form_item" label="外协类别:" prop="typeId">
            <div class="input_wapper">
              <el-select size="small" v-model="detailData.typeId" filterable placeholder="请选择外协类别">
                <el-option
                  v-for="item in companyType"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
        </div>
      </div>
      <two-title title="其他信息"></two-title>
      <div class="from_panel">
        <div class="form_col">
          <el-form-item class="form_item" label="单位地址:" prop="address">
            <div class="input_wapper">
              <el-input
                size="small"
                placeholder="请输入单位地址"
                v-model.trim="detailData.address"
                clearable>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="form_item" label="电话:" prop="phone">
            <div class="input_wapper">
              <el-input
                size="small"
                placeholder="请输入电话"
                v-model.trim="detailData.phone"
                clearable>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="form_item" label="邮箱:" prop="email">
            <div class="input_wapper">
              <el-input
                size="small"
                placeholder="请输入邮箱"
                v-model.trim="detailData.email"
                clearable>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="form_item" label="传真:" prop="fax">
            <div class="input_wapper">
              <el-input
                size="small"
                placeholder="请输入传真"
                v-model.trim="detailData.fax"
                clearable>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item class="form_item" label=" ">
            <label></label>
            <div class="input_wapper mes_btn">
              <el-button type="primary" native-type="submit" @click="save">保存</el-button>
              <el-button @click="cancel">取消</el-button>
            </div>
          </el-form-item>
        </div>
        <div class="form_col">
          <el-form-item class="form_item" label="备注" prop="remarks">
            <div class="input_wapper text_area">
              <el-input type="textarea"
                        :autosize="{ minRows: 2, maxRows: 4}"
                        placeholder="请输入内容"
                        v-model="detailData.remarks"
                        maxlength="250"></el-input>
              <span class="text_length">{{detailData.remarks ? detailData.remarks.length : 0}}/250</span>
            </div>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <empty-page v-if="isEmpty&&!isLoading"></empty-page>
  </div>
</template>

<script>
import TwoTitle from '@/components/TwoTitle'
import EmptyPage from '@/components/EmptyPage'
import {validateName, validateNumber, validatePhone, validateEmail} from '@/utils/validate'

export default {
  name: 'ManufacturerDetail',
  data() {
    return {
      // 数据加载中
      isLoading: true,
      // 数据监听
      startWatch: false,
      // 数据是否为空
      isEmpty: false,
      // 表单格式验证
      rules: {
        typeId: [
          {required: true, message: '请选择外协类别', trigger: 'blur'}
        ],
        number: [
          {required: true, message: '请输入外协单位编号', trigger: 'blur'},
          {validator: validateNumber, message: '请输入正确的外协单位编号', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入外协单位名称', trigger: 'blur'},
          {validator: validateName, message: '请输入正确的外协单位名称', trigger: 'blur'}
        ],
        phone: [
          {validator: validatePhone, message: '请输入正确的固定电话', trigger: 'blur'}
        ],
        email: [
          {validator: validateEmail, message: '请输入正确的邮箱', trigger: 'blur'}
        ]
      }
    }
  },
  components: {
    TwoTitle,
    EmptyPage
  },
  watch: {
    detailData: {
      // 监听startWatch的时候判断当前页面是否有变化
      handler() {
        if (this.startWatch) {
          this.$store.commit('SetHasDetailChange', true)
        }
      },
      deep: true
    }
  },
  computed: {
    // 详情数据
    detailData() {
      return this.$store.state.manufacturer.detailData
    },
    // 字典获取的外协类别下拉列表
    companyType() {
      return this.$store.state.dict.companyType.labels
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 更新方法
    refresh() {
      this.init()
    },
    // 初始化页面数据
    init() {
      this.isLoading = true
      this.startWatch = false
      this.isEmpty = false
      Promise.all([this.getDetail(), this.getCompanyType()]).then(() => {
        this.isLoading = false
      }).catch(() => {
        this.isLoading = false
      })
    },
    // 详情数据
    getDetail() {
      let id = this.$route.query.id
      let postData = {
        method: 'get',
        module: 'manufacturer',
        state: 'detailData',
        url: '/a/company/company/get/' + id
      }
      if (!id) {
        postData.clean = true
      }
      if (!id) { postData.clean = true }
      return this.$store.dispatch('saveData', postData).then((res) => {
        this.startWatch = true
        if (id && !res) { this.isEmpty = true }
      }).catch(() => {
        this.isEmpty = true
      })
    },
    // 外协类别数据字典
    getCompanyType() {
      return this.$store.dispatch('saveData', {
        method: 'get',
        url: 'a/company/company/dict/companyType',
        module: 'manufacturer',
        state: 'companyType'
      })
    },
    // 保存
    save() {
      if (this.saveState === true) return
      this.$refs['listForm'].validate((valid) => {
        if (valid) {
          this.$store.commit('SetSaveState', true)
          let postData = {
            method: 'post',
            url: '/a/company/company/save',
            data: this.detailData,
            module: 'manufacturer',
            noCommit: true
          }
          let $route = this.$route
          this.$store.dispatch('saveData', postData).then(() => {
            this.saveSuccess($route)
          }).catch(() => {
            this.$store.commit('SetSaveState', false)
          })
        }
      })
    },
    // 保存成功回调
    saveSuccess($route) {
      this.$message.success({
        message: '保存成功',
        duration: 500,
        center: true,
        onClose: () => {
          this.$store.commit('SetHasDetailChange', false)
          this.$store.dispatch('delCurrentViews', $route).then(() => {
            this.$store.commit('SetSaveState', false)
            this.$router.push('/manufacturer')
            this.$store.commit('SetRefresh', true)
          })
        }
      })
    },
    // 取消关闭当前页面
    cancel() {
      let $route = this.$route
      this.backToList().then(() => {
        this.$store.dispatch('delCurrentViews', $route).then(
          this.$router.push('/manufacturer')
        )
      })
    }
  }
}
</script>

<style scoped lang="less">
</style>
